﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.SqlClient;
using System.Text;
using EKAABSInformationManagement.Database;

namespace EKAABSInformationManagement.Service
{
    public class CardService
    {
        private EKAASBIMEntities dbContext;
        private static readonly object lock_flag = new object();
        public CardService()
        {
            dbContext = new EKAASBIMEntities();
        }

        public List<cardInBell> getAllCard()
        {
            return dbContext.cardInBell.ToList();
        }
        public cardInBell getCardByAlias(string alias)
        {
            var query = from a in dbContext.cardInBell
                        where a.belongto == alias
                        select a;
            return query.FirstOrDefault();
        }

        public void insert(cardInBell card, out bool b)
        {
            b = false;
            var c = dbContext.cardInBell.SingleOrDefault(o => o.cardNo == card.cardNo || o.belongto == card.belongto);
            if (c != null)
            {
                b = true;
                return;
            }
            card.latestUpdateDate = DateTime.Now;
            dbContext.cardInBell.AddObject(card);
            dbContext.SaveChanges();
        }
        public void delete(string[] CardList)
        {
            foreach (string Card in CardList)
            {
                var d = dbContext.cardInBell.SingleOrDefault(o => o.cardNo == Card);
                if (d != null)
                {
                    dbContext.DeleteObject(d);
                }
            }
            dbContext.SaveChanges();
        }
        public void update(cardInBell card, out bool b)
        {
            b = false;

            cardInBell oldCard = dbContext.cardInBell.SingleOrDefault(o => o.cardNo == card.cardNo);
            if (oldCard == null)
            {
                b = true;
                return;
            }
            else
            {
                if (oldCard.belongto != card.belongto)
                {
                    var temp = dbContext.cardInBell.SingleOrDefault(o => o.belongto == card.belongto);
                    if (temp != null)
                    {
                        b = true;
                        return;
                    }
                }
            }
            oldCard.cardNo = card.cardNo;
            oldCard.status = card.status;
            oldCard.belongto = card.belongto;
            oldCard.latestUpdateDate = card.latestUpdateDate;
            dbContext.SaveChanges();
        }
        public void batchUpdate(string[] cardNoList, cardInBell c)
        {
            foreach (string s in cardNoList)
            {
                var d = dbContext.cardInBell.SingleOrDefault(o => o.cardNo == s);
                if (d != null)
                {
                    d.latestUpdateDate = c.latestUpdateDate;
                    d.status = c.status;
                }
            }
            dbContext.SaveChanges();
        }
        public void update(CardService a)
        {
            throw new NotImplementedException();
        }
        #region Auto mail
        public List<cardInBell> getPreOverdueCardInfo_By_Fixeddate(DateTime date1, DateTime date2)
        {
            lock (lock_flag)
            {
                var query = from n in dbContext.cardInBell
                            where n.status == 2 || (n.latestUpdateDate > date2 && n.latestUpdateDate < date1)
                            select n;
                return query.ToList();
            }
        }
        public List<cardInBell> getOverduedCardInfo_By_Fixeddate(DateTime date)
        {
            lock (lock_flag)
            {
                var query = from n in dbContext.cardInBell
                            where n.status == 3 || n.latestUpdateDate < date
                            select n;
                return query.ToList();
            }
        }
        public void update_CardStatus_By_ID(string id, byte status)
        {
            lock (lock_flag)
            {
                cardInBell card = dbContext.cardInBell.SingleOrDefault(o => o.cardNo == id);
                if (card == null)
                {
                    return;
                }
                if (card.status != status)
                {
                    card.status = status;
                    dbContext.SaveChanges();
                }
            }
        }
        public BellCard_mail getCardInfoForMail_By_ID(string id)
        {
            lock (lock_flag)
            {
                var query = from u in dbContext.aspnet_Users
                            join m in dbContext.aspnet_Membership on u.UserId equals m.UserId
                            join c in dbContext.cardInBell on u.UserName equals c.belongto
                            where c.cardNo == id
                            select new BellCard_mail
                            {
                                BellCardID = c.cardNo,
                                owner = u.UserName,
                                email = m.Email
                            };
                return query.FirstOrDefault();
            }
        }
        #endregion
    }
}
